﻿using System;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;
using System.Collections.Generic;

namespace GPC_Overlay
{
    public class PolygonEntity 
    {
        public string PolygonData { get; set; }

        public PolygonEntity(Polygon polygon)
        {
            ConvertFromPolygon(polygon);
        }

        public PolygonEntity(string polygon)
        {
            PolygonData = polygon;
        }

        public void ConvertFromPolygon(Polygon polygon)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, polygon);
                PolygonData = System.Convert.ToBase64String(ms.ToArray());
            }
        }

        public static string ConvertFromPolygonStatic(Polygon polygon)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, polygon);
                return System.Convert.ToBase64String(ms.ToArray());
            }
        }

        public Polygon ConvertToPolygon()
        {
            Polygon basePolygon = new Polygon();
            byte[] data = System.Convert.FromBase64String(PolygonData);
            using (MemoryStream ms = new MemoryStream(data))
            {
                BinaryFormatter bf = new BinaryFormatter();
                basePolygon = bf.Deserialize(ms) as Polygon;
            }
            return basePolygon;
        }

        public static Polygon ConvertToPolygonStatic(MemoryStream PolygonData)
        {
            Polygon basePolygon = new Polygon();
            BinaryFormatter bf = new BinaryFormatter();
            PolygonData.Seek(0, SeekOrigin.Begin);
            basePolygon = bf.Deserialize(PolygonData) as Polygon;
            return basePolygon;
        }

        public override string ToString()
        {
            return PolygonData.ToString();
        }
    }
    
}
